use std::time::Duration;

use jnim::JEnv;

use super::{Window, WinRef};
use crate::android;

pub struct WinEvent<'a>(pub &'a ());
impl<'a> types::IntoEvt<Window, types::Mouse> for WinEvent<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::Mouse> {
		// TODO
		None
	}
}
impl<'a> types::IntoEvt<Window, types::Wheel> for WinEvent<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::Wheel> {
		// TODO
		None
	}
}
pub struct WinKeyboard<'a>(pub &'a android::view::KeyEvent, pub &'a JEnv);
impl<'a> types::IntoEvt<Window, types::Keyboard> for WinKeyboard<'a> {
	fn to_evt(&self, _win: WinRef<'_>) -> Option<types::Keyboard> {
		let evt = self.0;
		let env = self.1;
		log::warn!("EvtKey======="); // TODO
		use android::view::KeyEvent;
		match evt.get_action(env)? {
			KeyEvent::ACTION_DOWN => None,
			KeyEvent::ACTION_UP => None,
			_ => None,
		}
	}
}
pub struct WinTouch<'a>(pub u8, pub &'a android::view::MotionEvent, pub &'a JEnv);
impl<'a> types::IntoEvt<Window, types::Touch> for WinTouch<'a> {
	fn to_evt(&self, win: WinRef<'_>) -> Option<types::Touch> {
		let evt = self.1;
		let env = self.2;
		log::warn!("EvtTouch======"); // TODO
		use android::view::MotionEvent;
		let phase = match evt.get_action(env)? {
			MotionEvent::ACTION_DOWN => types::Phase::Start,
			MotionEvent::ACTION_UP => types::Phase::End,
			MotionEvent::ACTION_MOVE => types::Phase::Update,
			MotionEvent::ACTION_CANCEL => types::Phase::Cancel,
			_ => return None,
		};
		let point = (evt.get_x(env)?, evt.get_y(env)?);
		let nanos = self.1.get_event_time(env).map(|v| v * 1_000_000);
		let nanos = nanos.unwrap_or(win.1.frame_nano());
		let time = Duration::from_nanos(nanos as _);
		let force = 0;
		Some(types::Touch::new(types::Button::Touch(self.0), phase, point, force, time))
	}
}
